package com.webapp.org.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.webapp.org.bean.DesignationBean;
import com.webapp.org.interfaces.IDesgination;
import com.webapp.org.util.condatabase.HBConfigure;

public class DesignationDAO implements IDesgination
{
	private SessionFactory sf = HBConfigure.getSessionFactory();
	private boolean result = false;

	@Override
	public boolean insertDesignation(DesignationBean designationBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.persist(designationBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception exception)
		{
			//System.out.println(exception);
			session.getTransaction().rollback();
			System.out.println("Error occured while inserting");
		} finally
		{
			session.close();
		}
		return result;
	}

	@Override
	public boolean updateDesignation(DesignationBean designationBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.update(designationBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Exception took pplace in update");
		} finally
		{
			session.close();
		}
		return result;
	}

	@Override
	public boolean deleteDesignation(int designationId)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
    		DesignationBean designationBean = (DesignationBean) session.get(
					DesignationBean.class, designationId);
			session.delete(designationBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("error while deleting");
		} finally
		{
			session.close();
		}
		return result;
	}

	@Override
	public List<DesignationBean> retrieveDesignation()
	{
		List<DesignationBean> designationList = null;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			designationList = session.createQuery("FROM DesignationBean").list();
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error while retriving Designation data");
		} finally
		{
			session.close();
		}
		return designationList;
	}

	@Override
	public DesignationBean getDesignation(int designationId)
	{
		// TODO Auto-generated method stub
		return null;
	}

}
